package evidences

import (
	"livefind/common/response"
	eviReq "livefind/service/evidences/request"
	"livefind/utils"
	"livefind/utils/logger"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type EvidenceApi struct {
}

// @Tags Evidences
// @Summary 创建证据
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.EvidenceCreateReq true "案件ID,证据编号,证据名,证据类型,原始路径"
// @Success 200 {object} response.MsgResponse "{"code":20000,"message":"新建证据成功"}"
// @Router /evidences/evidence [post]
func (e *EvidenceApi) CreateEvidence(c *gin.Context) {
	var r eviReq.EvidenceCreateReq
	if err := c.ShouldBindWith(&r, binding.JSON); err != nil {
		c.AbortWithStatusJSON(
			http.StatusInternalServerError,
			gin.H{"error": err.Error()})
		logger.Error(err.Error(), err)
		return
	}

	if r.SavePath == "" {
		response.FailWithMessage("没有上传文件", c)
		return
	}

	if err := utils.Verify(r, utils.CreateEvidenceVerify); err != nil {
		logger.Error(err.Error(), err)
		response.FailWithMessage(err.Error(), c)
		return
	}

	err := evidenceService.Create(r)
	if err != nil {
		logger.Error(err.Error(), err)
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithMessage("新建证据成功", c)
}

// @Tags Evidences
// @Summary 根据条件获取证据列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query request.EvidenceListReq true "案件ID"
// @Success 200 {object} response.Response{data=response.EvidenceListRsp{list=[]response.EvidenceInfoRsp,page=int,size=int},code=int,message=string} "获取案件列表API响应"
// @router /evidences/list [get]
func (e *EvidenceApi) ListEvidences(c *gin.Context) {
	var r eviReq.EvidenceListReq
	if err := c.ShouldBindWith(&r, binding.Query); err != nil {
		c.AbortWithStatusJSON(
			http.StatusInternalServerError,
			gin.H{"error": err.Error()})
		logger.Error(err.Error(), err)
		return
	}

	if err := utils.Verify(r, utils.ListEvidencesVerify); err != nil {
		logger.Error(err.Error(), err)
		response.FailWithMessage(err.Error(), c)
		return
	}

	rsp, err := evidenceService.List(r)
	if err != nil {
		logger.Error(err.Error(), err)
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithDetailed(rsp, "获取证据列表成功", c)
}

// @Tags Evidences
// @Summary 根据id获取证据信息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query request.EvidenceGetReq true "证据ID"
// @Success 200 {object} response.Response{data=response.EvidenceGetRsp,code=int,message=string} "根据id获取证据信息API响应"
// @router /evidences/list [get]
func (e *EvidenceApi) GetEvidence(c *gin.Context)  {
	var r eviReq.EvidenceGetReq
	if err:=c.ShouldBindWith(&r,binding.Query);err!=nil {
		c.AbortWithStatusJSON(
			http.StatusInternalServerError,
			gin.H{"error":err.Error()})
		return
	}

	if err:=utils.Verify(r,utils.GetEvidenceVerify);err!=nil {
		logger.Error(err.Error(),err)
		response.FailWithMessage(err.Error(),c)
		return
	}

	rsp,err:=evidenceService.GetById(r)
	if err!=nil {
		logger.Error(err.Error(),err)
		response.FailWithMessage(err.Error(),c)
		return
	}

	response.OkWithDetailed(rsp,"获取证据信息成功",c)
}

// @Tags Evidences
// @Summary 上传证据相关文件
// @Security ApiKeyAuth
// @accept multipart/form-datau
// @Produce application/json
// @Param fileName formData file true "证据的相关文件"
// @Success 200 {object} response.Response{data=response.UploadInfoRsp,code=int,message=string} "{"code":20000,"message":"新建证据成功"}"
// @Router /evidences/upload [post]
func (e *EvidenceApi) UploadFile(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		logger.Error(err.Error(), err)
		response.FailWithMessage(err.Error(), c)
		return
	}

	rsp, err := evidenceService.Upload(file)

	if err != nil {
		logger.Error(err.Error(), err)
		response.FailWithMessage(err.Error(), c)
		return
	}

	response.OkWithDetailed(rsp, "上传证据成功", c)
}
